home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / ginter.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  3KB  |  136 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. /* ginter.c: generate interface code */
  10. #define GEN
  11.  
  12. #include "hdr.h"
  13. #include "libhdr.h"
  14. #include "vars.h"
  15. #include "segment.h"
  16. #include "gvars.h"
  17. #include "ops.h"
  18. #include "type.h"
  19. #include "ifile.h"
  20. #include "setp.h"
  21. #include "libp.h"
  22. #include "miscp.h"
  23. #include "bmainp.h"
  24. #include "ginterp.h"
  25.  
  26. void geninter(Tuple to_bind)                                    /*;geninter*/
  27. {
  28. #ifdef SUPPORT_PRAGMA_INTERFACE
  29.     char *INT_DIR;    /* directory containing partially bound interpreter */
  30.     char *INT_FILE;    /* filename representing partially bound interpreter */
  31.     char *PROC_INTERFACE; /* temporary file containing procedure interface() */
  32.     extern char *LIBRARY_PREFIX; /* user library, defined in misc.c */
  33.     char *argp[80];
  34. #endif
  35.     char *code;
  36.     char dummy_array[80];
  37.     int  i, j, n, m, status;
  38.     FILE *file;
  39.     char *token, *ptr, *s;
  40.     char *exec_name;
  41. #ifdef IBM_PC
  42.     FILE *efopen();
  43. #endif
  44.  
  45.     /* generation of the procedure interface whith the branches of the case
  46.      * which have been stored in the tuple interfaced_procedures*/
  47.     code = "interface(procedure)\nint procedure;\n{\n";
  48.     code = strjoin(code, "extern float get_float_argument_value();\n");
  49.     code = strjoin(code,
  50.       "extern int cur_stackptr, *cur_stack;\n switch(procedure){\n");
  51.     n = tup_size(interfaced_procedures);
  52.     m = tup_size(to_bind);
  53.     for (i = 1; i <= n; i+=2) {
  54.         for (j = 1; j <= m; j++) {
  55.             if((int)interfaced_procedures[i] == unit_numbered(to_bind[j])) {
  56.                 code = strjoin(code, interfaced_procedures[i+1]);
  57.                 break;
  58.             }
  59.         }
  60.     }
  61.     sprintf(dummy_array,
  62.       "\tdefault: raise(%d, \"Interface\");\n}\n}\n", 6);
  63.     code = strjoin(code, dummy_array);
  64. #ifdef SUPPORT_PRAGMA_INTERFACE
  65.     file = efopenl("interface.c", "", "w", "t");
  66. #endif
  67. #ifdef IBM_PC
  68.     file = efopen("iface.c", "w", "t");
  69. #endif
  70.     fprintf(file, code);
  71.     fclose(file);
  72.  
  73. #ifdef SUPPORT_PRAGMA_INTERFACE
  74.     PROC_INTERFACE = strjoin(LIBRARY_PREFIX, DIR_DELIMITER);
  75.     PROC_INTERFACE = strjoin(PROC_INTERFACE, "interface.c");
  76.  
  77.     /* construction of the array argp which is a parameter of execvp */
  78.     argp[0] = "cc";
  79.     argp[1] = PROC_INTERFACE;
  80.  
  81.     INT_DIR = getenv("INT");
  82.     if (INT_DIR == (char *) 0) {
  83.         user_error("environment variable  INT not set");
  84.         return;
  85.     }
  86.     INT_FILE = strjoin(INT_DIR, "/adaint");
  87.     argp[2] = INT_FILE;
  88.  
  89.     ptr = interface_files;
  90.     i = 3;
  91.     /* the string interface_files consists of a succession of tokens 
  92.      * followed by one blank. We break this string into tokens and we check 
  93.      * whether these tokens contain an 'o' extension or not 
  94.      */
  95.     while (*ptr != '\0') {
  96.         token = ptr;
  97.         while (*ptr != ' ') ptr++;
  98.         *ptr++ = '\0';
  99.         if (strchr(token, '.') == (char *)0) {
  100.             token = strjoin("-l", token);
  101.             argp[i++] = token;
  102.         }
  103.         else {
  104.             s = strchr(token, '.');
  105.             if ((*(s + 1) == 'o') && (*(s + 2) == '\0')) {
  106.                 argp[i++] = token;
  107.             }
  108.             else {
  109.                 sprintf(dummy_array, "%s not an object file",token);
  110.                 user_error(dummy_array);
  111.                 return;
  112.             }
  113.         }
  114.     }
  115.  
  116.     argp[i++] = "-lm";
  117.  
  118.     exec_name = strjoin(LIBRARY_PREFIX, DIR_DELIMITER);
  119.     exec_name = strjoin(exec_name, AISFILENAME);
  120.     exec_name = strjoin(exec_name, ".exe");
  121.     argp[i++] = "-o";
  122.     argp[i++] = exec_name;
  123.  
  124.     argp[i] = (char *) 0;
  125.  
  126.     if (fork() == 0) {
  127.         exit(execvp("gcc", argp));
  128.     }
  129.     else {
  130.         wait(&status);
  131.         unlink(PROC_INTERFACE);
  132.         unlink("interface.o");
  133.     }
  134. #endif
  135. }
  136.